package Rozetka.Pages;

import Rozetka.Pages.BasePage;
import Rozetka.ReportWriter.ReportWriter;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import java.util.NoSuchElementException;

public class CheckOutPage extends BasePage {

    private static By name = By.id("reciever_name");
    private static By locality = By.id("suggest_locality");
    private static By phone = By.id("reciever_phone");
    private static By email = By.id("reciever_email");
    private static By sumbitButton = By.name("next_step");

    public CheckOutPage(WebDriver driver) {
        super(driver);
    }

    public void fillFields (String userName, String phoneNumber, String emailAddress) {
        ReportWriter.info("Fill fields for order");
        try {
            driver.findElement(name).clear();
            driver.findElement(name).sendKeys(userName);
            driver.findElement(locality).clear();
            driver.findElement(By.cssSelector("li[name=\"0\"]")).click();
            driver.findElement(phone).clear();
            driver.findElement(phone).sendKeys(phoneNumber);
            driver.findElement(email).clear();
            driver.findElement(email).sendKeys(emailAddress);
        } catch (NoSuchElementException e) {
            ReportWriter.error("Didn't find element in sumbit order");
        }
    }

    public boolean sumbitOrder() {
        ReportWriter.info("Positive order start");

        if( !driver.findElement(By.id("make-order")).isEnabled()) {
            ReportWriter.error("Sumbit order is failed: Order button is not available");
            //cleanBasket();
            return false;
        }
            ReportWriter.info("Positive order is done");
           // cleanBasket();
            return true;
    }

    public boolean sumbitOrderNegative() {
        ReportWriter.info("Negative order start");

        if (!driver.findElement(By.name("email_msg")).isEnabled()) {
            ReportWriter.error("Error message is not appeared");
            //cleanBasket();
            return false;
        }
        ReportWriter.info("Negative order start finished correctly");
        //cleanBasket();
        return true;
    }


    public boolean sumbitOrderNegativePhoneSQL() throws InterruptedException {
        ReportWriter.info("Negative order start SQL");

        try {
            driver.findElement(sumbitButton).click();
        } catch (Exception e) {
            ReportWriter.info("SQL Negative order is done");
            //cleanBasket();
            return true;
        }
        //cleanBasket();
        return false;
    }

    public boolean negativeOnlineOrderEmptyRecycleBin() {
        ReportWriter.info("Negative order start - Empty Recycle Bin");
        try {
            driver.findElement(By.className("empty")).isDisplayed();
            //cleanBasket();
            return true;
        } catch (Exception e) {
            //cleanBasket();
            return false;
        }

    }



}
